/**
 * DSM - Distributed Search Manager
 * Developed by Milspec Research International Pty Ltd
 * $Author: skahl $
 * $Revision: 1.2 $
 * $Date: 2007/11/27 23:43:55 $
 * (c)Copyright 2004
 * education.au limited
 * DEST
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the names education.au limited, DEST nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * To the extent permitted by law, the copyright owners of this software and
 * its contributors:
 * (i) exclude all warranties in relation to the software; and
 * (ii) exclude liability for all losses, costs, expenses and damages arising
 * in any way from the use of the software whether arising from or in
 * relation to breach of contract, negligence or any other tort, in equity
 * or otherwise. If the software is in breach of a warranty which is
 * implied by law, the copyright owners and contributors liability is
 * limited to the replacement of the software.
 *
 * @author	gsingh
 */

package au.edu.educationau.opensource.dsm.worker;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;

import au.edu.educationau.opensource.dsm.service.PropertiesService;
import au.edu.educationau.opensource.dsm.util.Flog;

/**
 * Provides adapter grouping information - ie groups of repositories for
 * searching or passing parameters to.
 * 
 * Adapter groups are specified in the properties file, eg the line <code>
 * apapter.group.west=sa,wa,nt
 * </code>
 * defines the group 'west' which contains the adapters named 'sa', 'wa' and
 * 'nt'. These three repositories can then be searched simply by passing HTTP
 * query param sr=west, and a custom param can be passed to all three
 * repositories by passing west.param=value.
 * 
 * The value 'all' can be used in the properties file to assign all available
 * adapters to a group.
 * 
 * Note: If a group has the same name as an existing adapter the group will be
 * ignored.
 */

public final class AdapterGroups {
	/**
	 * Map of group names to String arrays containing the adapter names for each
	 * group. *
	 */
	private static Map groups = new HashMap(); // increase this number to
												// number of adapter types

	/** All of the adapter instances, comma delimited */
	public static final String ADAPTER_GROUP_PROPERTY_PREFIX = "adapter.group";

	/** Class name display */
	public final static String classStr = "o.m.d.w.AdapterGroups";

	/** Destroy objects worthy of destruction */
	public static void destroy() {
		groups.clear();
		Flog.debug(classStr, "Adapter Groups shutdown ok.");
	}

	/** Initialise the groups. */
	public static void init() {
		Flog.console(classStr, "Building Adapter Groups...");
		groups.clear();

		Properties props = PropertiesService.getAllProperties(ADAPTER_GROUP_PROPERTY_PREFIX);
		for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
			String name = (String) e.nextElement();
			if (SearchAdapterFactory.adapterExists(name)) {
				Flog.console(classStr, "Adapter Group " + name + " has not been created as there is already an adapter with that name.");
			} else {
				String propValue = PropertiesService.getStringProperty(ADAPTER_GROUP_PROPERTY_PREFIX + "." + name, "");
				String[] adapterNames = null;
				if (propValue.equals("all")) {
					adapterNames = SearchAdapterFactory.getSources();
				} else {
					adapterNames = propValue.split(",");
				}
				groups.put(name, adapterNames);
				Flog.console(classStr, "Adapter Group " + name + " built with " + ArrayUtils.toString(adapterNames));
			}
		}
	}

	/**
	 * Returns the names of all adapter groups. Returns an empty set if none
	 * exist.
	 */
	public static Set getGroups() {
		return groups.keySet();
	}

	/**
	 * Returns the names of all adpaters for a given group. Returns null if no
	 * such group exists.
	 */
	public static String[] getAdapterNames(String group) {
		return (String[]) groups.get(group);
	}
}
